package com.mstore.service;

import java.util.ArrayList;
import java.util.List;

import com.mstore.dao.ReplyDao;
import com.mstore.pojo.Reply;

public class ReplyService {

	private ReplyDao replyDao;

	/**
	 *  查询全部回复
	 */
	public List<Reply> watchReply(Reply reply) {
		List<Reply> replies = new ArrayList<Reply>();
		List<Object> objects = replyDao.watchReply(reply);
		for (Object object : objects) {
			Reply re = (Reply) object;
			re.setCreateTime(re.getCreateTime().substring(0,
					re.getCreateTime().indexOf(".")));
			if (re.getTitle() != null && re.getTitle().length() > 15) {
				re.setTitle(re.getTitle().substring(0, 15) + "...");
			}
			replies.add(re);
		}
		return replies;
	}

	/**
	 * 
	 * 查询菜单可选回复
	 */
	public List<Reply> watchMenuReply(Reply reply) {
		List<Reply> replies = new ArrayList<Reply>();
		List<Object> objects = replyDao.watchMenuReply(reply);
		for (Object object : objects) {
			Reply re = (Reply) object;
			re.setCreateTime(re.getCreateTime().substring(0,
					re.getCreateTime().indexOf(".")));
			if (re.getTitle() != null && re.getTitle().length() > 15) {
				re.setTitle(re.getTitle().substring(0, 15) + "...");
			}
			replies.add(re);
		}
		return replies;
	}

	/**
	 * 
	 * 通过type查询回复
	 */
	public List<Reply> watchReplyByType(Reply reply) {
		List<Reply> replies = new ArrayList<Reply>();
		List<Object> objects = replyDao.watchReplyByType(reply);
		for (Object object : objects) {
			Reply re = (Reply) object;
			// re.setCreateTime(re.getCreateTime().substring(0,
			// re.getCreateTime().indexOf(".")));
			replies.add(re);
		}
		return replies;
	}

	/**
	 * 
	 * 保存回复
	 */
	public void saveReply(Reply reply) {
		replyDao.saveReply(reply);
	}

	/**
	 * 
	 * 通过id查询回复
	 */
	public Reply getOneReply(Reply reply) {
		List<Object> objects = replyDao.getOneReply(reply);
		if (objects == null) {
			return null;
		}
		return (Reply) objects.get(0);
	}

	/**
	 * 
	 * 修改回复
	 */
	public void modifyReply(Reply reply) {
		replyDao.modifyReply(reply);
	}

	/**
	 * 
	 * 删除回复
	 */
	public void delReply(Reply reply) {
		replyDao.delReply(reply);
	}

	/**
	 * 
	 *查询关注时回复
	 */
	public Reply watchSubscribeReply(Reply reply) {
		List<Object> objects = replyDao.watchSubscribeReply(reply);
		if (objects == null || objects.size() == 0) {
			return null;
		}
		Reply re = (Reply) objects.get(0);
		re.setCreateTime(re.getCreateTime().substring(0,
				re.getCreateTime().indexOf(".")));
		if (re.getContent() != null && re.getContent().length() > 30) {
			re.setContent(re.getContent().substring(0, 30) + "...");
		}
		return re;
	}

	/**
	 * 
	 * 修改关注时回复
	 */
	public void modifySubscribeReply(Reply reply) {
		replyDao.modifySubscribeReply(reply);
	}

	/**
	 * 
	 * 修改多图文
	 */
	public void update(Reply reply) {
		replyDao.update(reply);
	}

	public ReplyDao getReplyDao() {
		return replyDao;
	}

	public void setReplyDao(ReplyDao replyDao) {
		this.replyDao = replyDao;
	}

}
